package mis.repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;

import mis.domain.User;

public class JdbcUserDao extends NamedParameterJdbcDaoSupport implements UserDao {

    /** Logger for this class and subclasses */
    protected final Log logger = LogFactory.getLog(getClass());


    public List<User> getUserList() {
        logger.info("DAO Getting user list!");
        List<User> users = getJdbcTemplate().query(
                "select * from user", 
                new UserMapper());
        return users;
    }

    public User getUser(int userId) {
        logger.info("DAO  Getting user:" + userId);
        List<User> users = getJdbcTemplate().query(
                "select * from user where userId=" + userId, 
                new UserMapper());
        
        User user = new User();
        if(users.size() >0)
        {
        	user = users.get(0);
        }
        return user;
    }
    
    public void updateUser(User user) {
        logger.info("DAO update user: " + user);
        int count = getNamedParameterJdbcTemplate().update(
            "update user set name = :name, password = :password, description = :description, " +
            "phoneNo = :phoneNo, email = :email, createTime = :createTime, updateTime= :updateTime  where userId = :userId",
            new MapSqlParameterSource()
            	.addValue("name", user.getName())
                .addValue("password", user.getPassword())
                .addValue("description", user.getDescription())
                .addValue("phoneNo", user.getPhoneNo())               
                .addValue("email", user.getEmail())
                .addValue("createTime", user.getCreateTime())
                .addValue("updateTime", user.getUpdateTime())
                .addValue("userId", user.getUserId())
        	);
        logger.info("Rows affected: " + count);
    }
 
    public void addUser(User user) {
        logger.info("DAO add user: " + user);
        int count = getNamedParameterJdbcTemplate().update(
            "insert into user(name,password,description,phoneNo,email,createTime,updateTime) values(:name,:password,:description," +
            ":phoneNo, :email, :createTime, :updateTime)",
            new MapSqlParameterSource().addValue("name", user.getName())
                .addValue("password", user.getPassword())
                .addValue("description", user.getDescription())
                .addValue("phoneNo", user.getPhoneNo())               
                .addValue("email", user.getEmail())
                .addValue("createTime", user.getCreateTime())
                .addValue("updateTime", user.getUpdateTime())     
        	);
        logger.info("Rows affected: " + count);
    }
    
    public void deleteUser(int userId) {
        logger.info("DAO delete user: " + userId);
        int count = getNamedParameterJdbcTemplate().update(
            "delete from user where userId = :userId",
            new MapSqlParameterSource().
            	addValue("userId", userId)    
        	);
        logger.info("Rows affected: " + count);
    }
    
    private static class UserMapper implements ParameterizedRowMapper<User> {

        public User mapRow(ResultSet rs, int rowNum) throws SQLException {
        	User user = new User();
        	user.setUserId(rs.getInt("userId"));
        	user.setName(rs.getString("name"));
        	user.setPassword(rs.getString("password"));
        	user.setDescription(rs.getString("description"));
        	user.setPhoneNo(rs.getString("phoneNo"));
        	user.setEmail(rs.getString("email"));
        	user.setCreateTime(rs.getDate("createTime"));
        	user.setUpdateTime(rs.getDate("updateTime"));
            return user;
        }

    }

}